package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.fasterxml.jackson.databind.ObjectMapper;
import com.qianfeng.es.service.DocService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/6/20 15:05
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
public class DocServiceImpl implements DocService {

    private RestHighLevelClient restHighLevelClient;

    private ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public void addDoc(String index, String json) throws Exception {
        //创建request,指定要添加的是哪个index,数据内容是什么格式
        IndexRequest request = new IndexRequest(index).source(json, XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.err.println(objectMapper.writeValueAsString(response));
    }

    @Override
    public void addDoc(String index, String id, String json) throws Exception {
        //创建request,指定要添加的是哪个index,数据内容是什么格式
        IndexRequest request = new IndexRequest(index)
                .id(id)//设置id
                .source(json, XContentType.JSON);//设置数据
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.err.println(objectMapper.writeValueAsString(response));
    }

    @Override
    public void getDoc(String index, String id) throws Exception {
        GetRequest getRequest = new GetRequest(index, id);
        GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.err.println(response.getSourceAsString());
    }

    @Override
    public void updateDoc(String index, String id, String json) throws IOException {
        //更新数据,指定index,和doc的id,已经要更新的内容
        UpdateRequest request = new UpdateRequest(index, id).doc(json, XContentType.JSON);
        UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        System.err.println(objectMapper.writeValueAsString(response));
    }

    @Override
    public void deleteDoc(String index, String id) throws Exception {
        DeleteRequest deleteRequest = new DeleteRequest(index, id);
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.err.println(objectMapper.writeValueAsString(response));
    }

    @Override
    public void bulkDoc(BulkRequest bulkRequest) throws Exception {
        BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//        System.err.println(objectMapper.writeValueAsString(response));
        System.err.println(response.toString());
    }

}
